package com.usts.ms.web.controller;

import com.usts.ms.model.BaseDict;
import com.usts.ms.model.Customer;
import com.usts.ms.model.QueryVo;
import com.usts.ms.service.BaseDictService;
import com.usts.ms.service.CustomerService;
import com.usts.ms.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by lenovo on 2019/5/14.
 * 客户信息请求处理
 */
@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private CustomerService customerService;

    @Value("${customer_from_type}")
    private String customer_from_type;

    @Value("${customer_industry_type}")
    private String customer_industry_type;

    @Value("${customer_level_type}")
    private String customer_level_type;

    @RequestMapping("list")
    public String list(Model model, QueryVo vo){

        //查询来源
        List<BaseDict> fromType = baseDictService.getBaseDictByCode(customer_from_type);
        //查询行业
        List<BaseDict> industryType = baseDictService.getBaseDictByCode(customer_industry_type);
        //查询级别
        List<BaseDict> levelType = baseDictService.getBaseDictByCode(customer_level_type);

        //设置数据模型的返回
        model.addAttribute("fromType",fromType);
        model.addAttribute("industryType",industryType);
        model.addAttribute("levelType",levelType);

        //跟据查询条件分页查询用户列表
        Page<Customer> page = customerService.getCustomerByQueryVo(vo);
        System.out.println("page:==========="+page);
        ////设置分页数返回
        model.addAttribute("page", page);
        //
        ////返回查询条件
        model.addAttribute("vo", vo);

        return "customer";
    }

    @RequestMapping("edit")
    @ResponseBody
    public Customer edit(Integer id){
        Customer customer = customerService.getCustomerById(id);
        System.out.println("customer=================="+customer);
        return customer;
    }

    @RequestMapping("mytag")
    public String myTag(){
        return "mytag";
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(Customer customer){
        customerService.updateCustomer(customer);
        return "ok";
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer id){
        customerService.deleteCustomerById(id);
        return "ok";
    }
}
